#include "StdAfx.h"

LoadingScreen* Singleton<LoadingScreen>::m_instance = 0;

LoadingScreen::LoadingScreen()
{
	m_lastRender = GetTickCount();
	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = 0;
	m_baseVertices[0].y = 0;
	m_baseVertices[0].u = 0.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = (float)sD3D.GetSize().x;
	m_baseVertices[1].y = 0;
	m_baseVertices[1].u = 1.0f;
	m_baseVertices[1].v = 0.0f;

	m_baseVertices[2].x = (float)sD3D.GetSize().x;
	m_baseVertices[2].y = (float)sD3D.GetSize().y;
	m_baseVertices[2].u = 1.0f;
	m_baseVertices[2].v = 1.0f;

	m_baseVertices[3].x = 0;
	m_baseVertices[3].y = (float)sD3D.GetSize().y;
	m_baseVertices[3].u = 0.0f;
	m_baseVertices[3].v = 1.0f;

	m_borderVertices[0].z = m_borderVertices[1].z = m_borderVertices[2].z = m_borderVertices[3].z = 0.0f;
	m_borderVertices[0].rhw = m_borderVertices[1].rhw = m_borderVertices[2].rhw = m_borderVertices[3].rhw = 1.0f;
	m_borderVertices[0].x = 256;
	m_borderVertices[0].y = 650;
	m_borderVertices[0].u = 0.0f;
	m_borderVertices[0].v = 0.0f;

	m_borderVertices[1].x = 768;
	m_borderVertices[1].y = 650;
	m_borderVertices[1].u = 1.0f;
	m_borderVertices[1].v = 0.0f;

	m_borderVertices[2].x = 768;
	m_borderVertices[2].y = 700;
	m_borderVertices[2].u = 1.0f;
	m_borderVertices[2].v = 1.0f;

	m_borderVertices[3].x = 256;
	m_borderVertices[3].y = 700;
	m_borderVertices[3].u = 0.0f;
	m_borderVertices[3].v = 1.0f;

	m_fillVertices[0].z = m_fillVertices[1].z = m_fillVertices[2].z = m_fillVertices[3].z = 0.0f;
	m_fillVertices[0].rhw = m_fillVertices[1].rhw = m_fillVertices[2].rhw = m_fillVertices[3].rhw = 1.0f;
	m_fillVertices[0].x = 276;
	m_fillVertices[0].y = 665;
	m_fillVertices[0].u = 0.25f;
	m_fillVertices[0].v = 0.0f;

	m_fillVertices[1].x = 750;
	m_fillVertices[1].y = 665;
	m_fillVertices[1].u = 0.75f;
	m_fillVertices[1].v = 0.0f;

	m_fillVertices[2].x = 750;
	m_fillVertices[2].y = 685;
	m_fillVertices[2].u = 0.75f;
	m_fillVertices[2].v = 1.0f;

	m_fillVertices[3].x = 276;
	m_fillVertices[3].y = 685;
	m_fillVertices[3].u = 0.25f;
	m_fillVertices[3].v = 1.0f;

	m_screenTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\GLUES\\LOADINGSCREENS\\LoadScreenAhnQiraj20man.blp");
	m_barBorder = CTextureHandler::GetInstance()->GetTexture("Interface\\GLUES\\LoadingBar\\Loading-BarBorder.blp");
	m_fillTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\GLUES\\LoadingBar\\Loading-BarFill.blp");

}

void LoadingScreen::Resize()
{
	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = 0;
	m_baseVertices[0].y = 0;
	m_baseVertices[0].u = 0.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = (float)sD3D.GetSize().x;
	m_baseVertices[1].y = 0;
	m_baseVertices[1].u = 1.0f;
	m_baseVertices[1].v = 0.0f;

	m_baseVertices[2].x = (float)sD3D.GetSize().x;
	m_baseVertices[2].y = (float)sD3D.GetSize().y;
	m_baseVertices[2].u = 1.0f;
	m_baseVertices[2].v = 1.0f;

	m_baseVertices[3].x = 0;
	m_baseVertices[3].y = (float)sD3D.GetSize().y;
	m_baseVertices[3].u = 0.0f;
	m_baseVertices[3].v = 1.0f;

	m_borderVertices[0].z = m_borderVertices[1].z = m_borderVertices[2].z = m_borderVertices[3].z = 0.0f;
	m_borderVertices[0].rhw = m_borderVertices[1].rhw = m_borderVertices[2].rhw = m_borderVertices[3].rhw = 1.0f;
	m_borderVertices[0].x = sD3D.GetSize().x / 2.0f - 256.0f;
	m_borderVertices[0].y = (float)sD3D.GetSize().y - 118;
	m_borderVertices[0].u = 0.0f;
	m_borderVertices[0].v = 0.0f;

	m_borderVertices[1].x = sD3D.GetSize().x / 2.0f + 256.0f;
	m_borderVertices[1].y = (float)sD3D.GetSize().y - 118;
	m_borderVertices[1].u = 1.0f;
	m_borderVertices[1].v = 0.0f;

	m_borderVertices[2].x = sD3D.GetSize().x / 2.0f + 256.0f;
	m_borderVertices[2].y = (float)sD3D.GetSize().y - 68;
	m_borderVertices[2].u = 1.0f;
	m_borderVertices[2].v = 1.0f;

	m_borderVertices[3].x = sD3D.GetSize().x / 2.0f - 256.0f;
	m_borderVertices[3].y = (float)sD3D.GetSize().y - 68;
	m_borderVertices[3].u = 0.0f;
	m_borderVertices[3].v = 1.0f;

	m_fillVertices[0].z = m_fillVertices[1].z = m_fillVertices[2].z = m_fillVertices[3].z = 0.0f;
	m_fillVertices[0].rhw = m_fillVertices[1].rhw = m_fillVertices[2].rhw = m_fillVertices[3].rhw = 1.0f;
	m_fillVertices[0].x = sD3D.GetSize().x / 2.0f - 256.0f + 20;
	m_fillVertices[0].y = (float)sD3D.GetSize().y - 103;
	m_fillVertices[0].u = 0.25f;
	m_fillVertices[0].v = 0.0f;

	m_fillVertices[1].x = sD3D.GetSize().x / 2.0f + 256.0f - 18.0f;
	m_fillVertices[1].y = (float)sD3D.GetSize().y - 103;
	m_fillVertices[1].u = 0.75f;
	m_fillVertices[1].v = 0.0f;

	m_fillVertices[2].x = sD3D.GetSize().x / 2.0f + 256.0f - 18.0f;
	m_fillVertices[2].y = (float)sD3D.GetSize().y - 83;
	m_fillVertices[2].u = 0.75f;
	m_fillVertices[2].v = 1.0f;

	m_fillVertices[3].x = sD3D.GetSize().x / 2.0f - 256.0f + 20;
	m_fillVertices[3].y = (float)sD3D.GetSize().y - 83;
	m_fillVertices[3].u = 0.25f;
	m_fillVertices[3].v = 1.0f;
}

void LoadingScreen::Render()
{
	if(sD3D.GetAppState() != STATE_LOADING)
	{
		m_tilesLoaded = 0;
		return;
	}
	sD3D->SetRenderState(D3DRS_ZENABLE, FALSE);
	sD3D->SetVertexShader(NULL);
	sD3D->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	sD3D->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	sD3D->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	sD3D->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

	m_fillVertices[1].x = (sD3D.GetSize().x / 2.0f - 256.0f + 20) + (474 * (m_tilesLoaded / (9.0f * 256.0f)));
	m_fillVertices[2].x = (sD3D.GetSize().x / 2.0f - 256.0f + 20) + (474 * (m_tilesLoaded / (9.0f * 256.0f)));

	sD3D->SetFVF(VERTEX3FRHWT);
	sD3D->SetTexture(0, *m_screenTexture);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_baseVertices, sizeof(Vertex3FRHWT));
	sD3D->SetTexture(0, *m_fillTexture);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_fillVertices, sizeof(Vertex3FRHWT));
	sD3D->SetTexture(0, *m_barBorder);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_borderVertices, sizeof(Vertex3FRHWT));
	sD3D->SetRenderState(D3DRS_ZENABLE, TRUE);
}

void LoadingScreen::SetMap(ui32 map)
{
	if(dbcLoadingScreen.LookupEntry(dbcMap.LookupEntry(map)->loadScreenId) != NULL) {
		auto tex = CTextureHandler::GetInstance()->GetTexture(dbcLoadingScreen.LookupEntry(dbcMap.LookupEntry(map)->loadScreenId)->path);
		if(tex != NULL)
			m_screenTexture = tex;
	}
}